
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation.Controls;


namespace Microsoft.NETMF.BikeComputer.Application
{
    public class UpDownArrowButton : ContentControl
    {
        Text _labelText;
        SettingsTable _settingsTable;
        Image _arrowUp;
        Image _arrowDown;
        bool _upSelected;
        bool _downSelected;

        //--//


        public UpDownArrowButton(SettingsTable settingsTable)
        {
            _settingsTable = settingsTable;

            StackPanel basePanel = new StackPanel(Orientation.Vertical);
            basePanel.SetMargin(2, 2, 2, 2);

            _arrowUp = new Image(Resources.GetBitmap(Resources.BitmapResources.UpArrow));
            _arrowDown = new Image(Resources.GetBitmap(Resources.BitmapResources.DnArrow));

            _labelText = new Text(Resources.GetFont(Resources.FontResources.miramob), _settingsTable.Current().Label.ToString());
            _labelText.SetMargin(0, 5, 0, 5);
            basePanel.Children.Add(_arrowUp);
            basePanel.Children.Add(_labelText);
            basePanel.Children.Add(_arrowDown);

            Child = basePanel;
        }


        protected override void OnTouchDown(TouchEventArgs e)
        {
            int xUp, yUp, xDn, yDn;

            e.GetPosition(_arrowUp, 0, out xUp, out yUp);
            e.GetPosition(_arrowDown, 0, out xDn, out yDn);

            lock (this)
            {
                _upSelected = false;
                _downSelected = false;

                if (xUp < _arrowUp.ActualWidth && yUp < _arrowUp.ActualHeight)
                {
                    _arrowUp.Bitmap = Resources.GetBitmap(Resources.BitmapResources.UpArrowOn);
                    _upSelected = true;
                }
                else if (xDn < _arrowDown.ActualWidth && yDn < _arrowDown.ActualHeight)
                {
                    _arrowDown.Bitmap = Resources.GetBitmap(Resources.BitmapResources.DnArrowOn);
                    _downSelected = true;
                }
            }
        }

        protected override void OnTouchUp(TouchEventArgs e)
        {
            bool upSelected;
            bool downSelected;

            lock (this)
            {
                upSelected = _upSelected;
                downSelected = _downSelected;

                if (upSelected)
                {
                    _arrowUp.Bitmap = Resources.GetBitmap(Resources.BitmapResources.UpArrow);
                    _upSelected = false;
                }
                else if (downSelected)
                {
                    _arrowDown.Bitmap = Resources.GetBitmap(Resources.BitmapResources.DnArrow);
                    _downSelected = false;
                }
            }

            string text = "";

            if (upSelected)
            {
                text = _settingsTable.Next().Label;
            }
            else if (downSelected)
            {
                text = _settingsTable.Previous().Label;
            }
            _labelText.TextContent = text;
        }
    }
}
